package Controller;

import Dao.AdministratorDao;
import Dao.SignupDao;
import Dao.StaffDao;
import Entity.Officer;
import Utils.SimpleTools;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

public class AdministratorController extends Application implements Initializable {
    Stage primaryStage = new Stage();
    Officer officer = new Officer();
    @FXML
    private AnchorPane settlePane;
    @FXML
    private Label namelabel;
    @FXML
    private Label nameLabel;
    @FXML
    private Label idLabel;
    @FXML
    private Label specialityLabel;
    @FXML
    private Label typeLabel;
    @FXML
    private Label phonecallLabel;
    @FXML
    private Button goback;
    @FXML
    private TextField nameTextField;
    @FXML
    private TextField specialityTextField;
    @FXML
    private TextField idTextField;
    @FXML
    private TextField typeTextField;
    @FXML
    private TextField phoneTextField;
    @FXML
    private TextField itspassword;
    @FXML
    private TextField itsusername;
    @FXML
    private TextField itsid;
    @FXML
    private TextField itsname;

    ObservableList<String> strList1 = FXCollections.observableArrayList(AdministratorDao.getnames(AdministratorDao.getOfficers("医生")));
    ObservableList<String> strList2 = FXCollections.observableArrayList(AdministratorDao.getnames(AdministratorDao.getOfficers("护士")));
    ObservableList<String> strList3 = FXCollections.observableArrayList(AdministratorDao.getnames(AdministratorDao.getOfficers("护工")));

    @FXML
    private ListView<String> doctorAdmin = new ListView<>(strList1);
    @FXML
    private ListView<String> nurseAdmin = new ListView<>(strList2);
    @FXML
    private ListView<String> assistantAdmin = new ListView<>(strList3);

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        doctorAdmin.setItems(strList1);
        nurseAdmin.setItems(strList2);
        assistantAdmin.setItems(strList3);
        String label = "尊敬的管理员"+ StaffDao.getInstance().getOfficer().getName()+"，您好";
        namelabel.setText(label);
    }

    public AdministratorController() throws IOException {}

    //修改个人信息
    @FXML
    void settle(MouseEvent event) {
        settlePane.setVisible(true);
    }
    @FXML
    void Finish(ActionEvent event) throws IOException {
        Officer it = AdministratorDao.getAdmin();
        Officer its = new Officer(it.getUsername(),it.getPassword(),it.getName(),
                it.getId(),it.getBirth(), it.getSpeciality(),it.getType(),
                it.getPhone(),it.getJurisdiction());

        if(!SimpleTools.isEmpty(itsname.getText())){

            its.setName(itsname.getText());
        }
        if(!SimpleTools.isEmpty(itsusername.getText())){
            if(SignupDao.iscontain(itsusername.getText())){
                SimpleTools.informationDialog(Alert.AlertType.WARNING, "提示", "抱歉", "该用户名已注册");
                itsusername.clear();
            }
            else its.setUsername(itsusername.getText());
        }
        if(!SimpleTools.isEmpty(itspassword.getText())){
            its.setPassword(itspassword.getText());
        }
        if(!SimpleTools.isEmpty(itsid.getText())){
            if(itsid.getText().length()!=18){
                SimpleTools.informationDialog(Alert.AlertType.WARNING, "提示", "警告", "请正确输入身份证号");
                itsid.clear();
            }
            else its.setId(itsid.getText());
        }
        settlePane.setVisible(false);

        namelabel.setText("尊敬的管理员"+ its.getName()+"，您好");

        AdministratorDao.replace(it,its);
    }

    @FXML
    void addButton(ActionEvent event) throws Exception {
        StaffDao.getInstance().setOperator(1);
        WithAdController withAdController = new WithAdController();
        withAdController.showWindow();
        strList1.clear();
        strList1.addAll(AdministratorDao.getnames(AdministratorDao.getOfficers("医生")));
        doctorAdmin.setItems(strList1);
        strList2.clear();
        strList2.addAll(AdministratorDao.getnames(AdministratorDao.getOfficers("护士")));
        nurseAdmin.setItems(strList2);
        strList3.clear();
        strList3.addAll(AdministratorDao.getnames(AdministratorDao.getOfficers("护工")));
        assistantAdmin.setItems(strList3);
    }

    @FXML
    void searchButton(ActionEvent event) throws Exception {
        StaffDao.getInstance().setOperator(2);
        WithAdController withAdController = new WithAdController();
        withAdController.showWindow();
    }

    @FXML
    void changeButton(ActionEvent event) throws Exception {
        Officer tmp = new Officer(officer.getUsername(),officer.getPassword(),officer.getName(),
                officer.getId(),officer.getBirth(), officer.getSpeciality(),officer.getType(),
                officer.getPhone(),officer.getJurisdiction());

        if(!SimpleTools.isEmpty(nameTextField.getText())){
            officer.setName(nameTextField.getText());
            nameLabel.setText(officer.getName());
        }
        if(!SimpleTools.isEmpty(specialityTextField.getText())){
            officer.setSpeciality(specialityTextField.getText());
            specialityLabel.setText(officer.getSpeciality());
        }
        if(!SimpleTools.isEmpty(idTextField.getText())){
            if(idTextField.getText().length()!=18) SimpleTools.informationDialog(Alert.AlertType.WARNING, "提示", "警告", "请正确输入身份证号");
            else{
                officer.setId(idTextField.getText());
                idLabel.setText(officer.getId());
            }
        }
        if(!SimpleTools.isEmpty(typeTextField.getText())){
            officer.setType(typeTextField.getText());
            typeLabel.setText(officer.getType());
        }
        if(!SimpleTools.isEmpty(phoneTextField.getText())){
            officer.setPhone(phoneTextField.getText());
            phonecallLabel.setText(officer.getPhone());
        }
        AdministratorDao.replace(tmp,officer);

        nameTextField.clear();
        idTextField.clear();
        specialityTextField.clear();
        typeTextField.clear();
        phoneTextField.clear();

        strList1 = FXCollections.observableArrayList(AdministratorDao.getnames(AdministratorDao.getOfficers("医生")));
        strList2 = FXCollections.observableArrayList(AdministratorDao.getnames(AdministratorDao.getOfficers("护士")));
        strList3 = FXCollections.observableArrayList(AdministratorDao.getnames(AdministratorDao.getOfficers("护工")));
        doctorAdmin.setItems(null);
        nurseAdmin.setItems(null);
        assistantAdmin.setItems(null);
        doctorAdmin.setItems(strList1);
        nurseAdmin.setItems(strList2);
        assistantAdmin.setItems(strList3);

    }
    @FXML
    void removeButton(ActionEvent event) throws IOException {
        if(SimpleTools.informationDialog(Alert.AlertType.CONFIRMATION,"提示","注意！","您确定要移除该职工吗？")){
            AdministratorDao.remove(officer);
            switch (officer.getType()){
                case "医生":{
                    strList1.remove(officer.getName());
                    doctorAdmin.refresh();
                    break;
                }
                case "护士":{
                    strList2.remove(officer.getName());
                    nurseAdmin.refresh();
                    break;
                }
                case "护工":{
                    strList3.remove(officer.getName());
                    break;
                }
            }

            officer = null;
            nameLabel.setText(null);
            idLabel.setText(null);
            specialityLabel.setText(null);
            typeLabel.setText(null);
            phonecallLabel.setText(null);


            strList1.clear();
            strList1.addAll(AdministratorDao.getnames(AdministratorDao.getOfficers("医生")));
            doctorAdmin.setItems(strList1);
            strList2.clear();
            strList2.addAll(AdministratorDao.getnames(AdministratorDao.getOfficers("护士")));
            nurseAdmin.setItems(strList2);
            strList3.clear();
            strList3.addAll(AdministratorDao.getnames(AdministratorDao.getOfficers("护工")));
            assistantAdmin.setItems(strList3);
        }
    }

    @FXML
    void goback(ActionEvent event) throws Exception {
        ((Stage)goback.getScene().getWindow()).close();
        (new LogFrameController()).showWindow();
    }

    @FXML
    void assistantManage(MouseEvent event) throws IOException {
        String assname = assistantAdmin.getSelectionModel().getSelectedItem();
        officer = AdministratorDao.byname(assname);
        if(officer!=null){
            nameLabel.setText(assname);
            idLabel.setText(officer.getId());
            specialityLabel.setText(officer.getSpeciality());
            typeLabel.setText(officer.getType());
            phonecallLabel.setText(officer.getPhone());
        }
    }
    @FXML
    void doctorManage(MouseEvent event) throws IOException {
        String assname = doctorAdmin.getSelectionModel().getSelectedItem();
        officer = AdministratorDao.byname(assname);
        if(officer!=null){
            nameLabel.setText(assname);
            idLabel.setText(officer.getId());
            specialityLabel.setText(officer.getSpeciality());
            typeLabel.setText(officer.getType());
            phonecallLabel.setText(officer.getPhone());
        }
    }
    @FXML
    void nurseManage(MouseEvent event) throws IOException {
        String assname = nurseAdmin.getSelectionModel().getSelectedItem();
        officer = AdministratorDao.byname(assname);
        if(officer!=null){
            nameLabel.setText(assname);
            idLabel.setText(officer.getId());
            specialityLabel.setText(officer.getSpeciality());
            typeLabel.setText(officer.getType());
            phonecallLabel.setText(officer.getPhone());
        }
    }

    @Override
    public void start(Stage primaryStage) throws Exception{
        this.primaryStage = primaryStage;

        Parent root = FXMLLoader.load(getClass().getResource("../View/AdministratorController.fxml"));
        primaryStage.setTitle("颐养中心");
        primaryStage.setScene(new Scene(root, 1200, 720));
        primaryStage.getIcons().add(new Image("file:src/resource/flour.png"));
        primaryStage.show();
    }
    public void  showWindow() throws Exception {
        start(primaryStage);
    }
    public static void main(String[] args) {
        launch(args);
    }

}
